﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ledger
{
    public partial class LedgerHost : Form
    {
        private List<Ledger> LedgerList { get; set; }

        private void Populate()
        {
            listLedgers.Items.Clear();
            foreach (Ledger l in LedgerList)
            {
                ListViewItem lvi = new ListViewItem(l.Description);
                lvi.SubItems.Add(l.From.ToShortDateString());
                lvi.SubItems.Add(l.To.ToShortDateString());
                listLedgers.Items.Add(lvi);
            }
        }

        public LedgerHost()
        {
            InitializeComponent();
            this.LedgerList = new List<Ledger>();
        }

        private void toolNewLedger_Click(object sender, EventArgs e)
        {
            LedgerDialog dlg = new LedgerDialog();
            if (dlg.ShowDialog()== System.Windows.Forms.DialogResult.OK)
            {
                this.LedgerList.Add(dlg.Info);
                Populate();
            }
        }

        private void toolEditLedger_Click(object sender, EventArgs e)
        {
            int index = listLedgers.SelectedIndices[0];
            if (index >= 0)
            {
                LedgerDialog dlg = new LedgerDialog();
                dlg.Info = this.LedgerList[index];
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.LedgerList[index] = dlg.Info;
                    Populate();
                }
            }
        }
    }
}
